package com.tencent.examples.blueking.extend;

import com.tencent.bk.utils.blueking.tools.CommonSugar;
import com.tencent.bk.utils.blueking.tools.StringConverters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unused")
public class QueryParam {

    private static final Logger logger = LoggerFactory.getLogger(QueryParam.class);
    private static final Integer defaultPageSize = 20;

    /**
     * 分页参数
     */
    private Integer pageNow;
    private Integer pageSize;

    public Integer getPageNow() {
        return CommonSugar.getTypedDefault(pageNow, 0);
    }

    public void setPageNow(Integer pageNow) {
        this.pageNow = pageNow;
    }

    public Integer getPageSize() {
        return CommonSugar.getTypedDefault(pageSize, 0);
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 自定义SQL（SQL标识，SQL内容）
     */
    private Map<String, String> sqlMap;

    public Map<String, String> getSqlMap() {
        if (sqlMap == null) {
            sqlMap = new HashMap<>();
        }
        return sqlMap;
    }

    public void setSqlMap(Map<String, String> sqlMap) {
        this.sqlMap = sqlMap;
    }

    public void clearSqlMap() {
        this.sqlMap = new HashMap<>();
    }

    /**
     * 查询排序
     */
    public String sortby;
    /**
     * 排序规则
     */
    public Sort.Direction sort;


    public String getSortby() {
        return sortby;
    }

    public void setSortby(String sortby) {
        this.sortby = sortby;
    }

    public Sort.Direction getSort() {
        return sort;
    }

    public void setSort(Sort.Direction sort) {
        this.sort = sort;
    }

    /**
     * HttpServletRequest & HttpServletResponse
     */
    public HttpServletRequest request;
    public HttpServletResponse response;

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * *********************************************************************************
     * *********************************************************************************
     * 构造函数
     * *********************************************************************************
     * *********************************************************************************
     */
    public QueryParam() {
    }

    public QueryParam(String pageNowParam, String pageSizeParam) {

        Integer pageNow = StringConverters.ToInteger(pageNowParam);
        Integer pageSize = StringConverters.ToInteger(pageSizeParam);

        if (pageNow == null || pageSize == null) {
            pageNow = 1;
            pageSize = defaultPageSize;
        }

        this.pageNow = pageNow;
        this.pageSize = pageSize;
    }

    public QueryParam(Integer pageNow, Integer pageSize) {
        this.pageNow = CommonSugar.getTypedDefault(pageNow, 1);
        this.pageSize = CommonSugar.getTypedDefault(pageSize, defaultPageSize);
    }

    /**
     * 设置查询的分页参数
     *
     * @param query
     * @param queryParam
     */
    public static void pagination(Query query, QueryParam queryParam) {
        if (queryParam.getPageNow() == null || queryParam.getPageSize() == null || queryParam.getPageNow() <= 0 || queryParam.getPageSize() <= 0) {
            return;
        }
        query.skip((queryParam.getPageNow() - 1) * queryParam.getPageSize());// 从那条记录开始
        query.limit(queryParam.getPageSize());// 取多少条记录
    }
}
